<?php 
	class Users extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

		/**
		* 函数功能介绍：获取联合登录数据
		* 创建人: Zhou hailong
		* 创建时间: 2016-01-21 16:11
		* 最后修改人：
		* 最后修改时间：
		*/
		function get_open_uid($open_id, $type){
			$sql = "select id, uid from {$this->db->dbprefix}third_party where open_id = '{$open_id}' and type='{$type}'";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		function get_user_childer_list($limit = 0, $offset = 10, $conditions = array()){
			$where = "1=1";
			if(!empty($conditions['user_id'])){
				$where .= " AND user_id='{$conditions['user_id']}' ";
			}
			$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}user_childrens WHERE $where ORDER BY id DESC LIMIT $limit, $offset";
            $result = $this->db->query($sql)->result_array();
			$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
			return array("count" => $row_num['row_num'], "list" => $result);
		}

		//获取孩子某个任务的统计数据(主要统计某个任务累计获得多少星，最高连续多少天，已连续多少天)
		function get_user_children_task_type_statistics($child_id, $task_id){
			$sql = "SELECT * FROM {$this->db->dbprefix}user_children_task_type_statistics WHERE child_id = '{$child_id}' AND task_id = '{$task_id}'";
			$result = $this->db->query($sql)->row_array();
			return $result;
		}

		function get_child_dynamics($child_id, $conditions = array()){
			$where = "child_id = '{$child_id}'";
			if(!empty($conditions['type'])){
				$where .= " AND type = '{$conditions['type']}' ";
			}
			if(!empty($conditions['category_id'])){
				$where .= " AND category_id = '{$conditions['category_id']}' ";
			}
			$sql = "SELECT id,type,data_id,category_id,stars,text,image,FROM_UNIXTIME(add_time) AS add_time FROM {$this->db->dbprefix}user_dynamics WHERE $where ORDER BY id DESC ";
			$result = $this->db->query($sql)->result_array();
			return $result;
		}
	}